from uuid import UUID

from pocketbase.services import AdminAuthResponse

from admins.utils import form_user_body, form_updated_user_body
from application.utils import get_rich_logger
from application.handle_exception import pb_operation_exception
from db.metastore import connect_with_pocketbase, admin_auth_via_client, client_create_new, client_search_first_item, \
    client_update_to, client_delete_record, user_auth_via_client
from domain.authentication import User, Admin

logger = get_rich_logger()


async def create_new_user(
        new_user: User,
        authenticated_admin: Admin,
):
    with connect_with_pocketbase() as db_client:
        auth_rsp: AdminAuthResponse = admin_auth_via_client(
            client=db_client,
            admin=authenticated_admin
        )
        logger.debug(f"Metastore admin authentication done. Response: {auth_rsp.admin, auth_rsp.token}")
        # create new user
        user_body = form_user_body(user=new_user)
        user_create_response = await client_create_new(
            client=db_client,
            collection="users",
            data=user_body
        )
        if not user_create_response:
            raise pb_operation_exception
        logger.debug(f"New admin created.")
    return user_create_response


async def fetch_user_info_record(
        user_uuid: UUID,
        user: User,
):
    with connect_with_pocketbase() as db_client:
        _ = user_auth_via_client(
            client=db_client,
            user=user
        )
        user_record_response = await client_search_first_item(
            client=db_client,
            collection="users",
            filter_predicate=f"uuid = '{user_uuid.hex}'"
        )
        if not user_record_response:
            raise pb_operation_exception
        logger.debug(f"Fetch user information complete.")
    return user_record_response


async def update_user_info_record(
        new_user_info: User,
        record_id: str,
        user: User,
):
    logger.debug(f"New user info: {new_user_info}")
    with connect_with_pocketbase() as db_client:
        _ = user_auth_via_client(
            client=db_client,
            user=user
        )
        # form new user body
        new_user_body = form_updated_user_body(
            new_user=new_user_info,
            current_user=user
        )
        user_update_response = await client_update_to(
            client=db_client,
            collection="users",
            target_id=record_id,
            data=new_user_body
        )
        if not user_update_response:
            raise pb_operation_exception
        logger.debug(f"User info successfully updated.")
    return user_update_response


async def delete_user_record(
        record_id: str,
        authenticated_admin: Admin,
):
    with connect_with_pocketbase() as db_client:
        auth_rsp: AdminAuthResponse = admin_auth_via_client(
            client=db_client,
            admin=authenticated_admin
        )
        logger.debug(f"Metastore admin authentication done. Response: {auth_rsp.admin, auth_rsp.token}")
        user_delete_response = await client_delete_record(
            client=db_client,
            collection="users",
            target_id=record_id
        )
        if not user_delete_response:
            raise pb_operation_exception
        logger.debug(f"User info successfully deleted.")
    return user_delete_response
